package servletsWithoutStruts;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sun.xml.internal.bind.CycleRecoverable.Context;

import domain.IDAOPhoneNumber;

/**
 * Servlet implementation class CreatePhoneServlet
 */
public class CreatePhoneServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CreatePhoneServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("contactId", request.getParameter("contactId"));
		request.getRequestDispatcher("createPhoneNumber.jsp").forward(request, response);	

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String contactId = request.getParameter("contactId");
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		IDAOPhoneNumber daoPhone = (IDAOPhoneNumber) context.getBean("daophonenumber");
		daoPhone.addPhoneToContact( Long.valueOf(contactId), request.getParameter("phoneKind"), request.getParameter("phoneNumber"));
		request.setAttribute("id", contactId);
		response.sendRedirect(response.encodeRedirectURL("ModifyContactServlet?id=" + contactId));
	}
}


